﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyUAMS.BLL;
using MyUAMS.DAL;

public partial class MaintenanceMgm_MaintenanceForm : System.Web.UI.Page
{
    MaintenanceServer MSrv = new MaintenanceServer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Aid"] == null)
        {
            lblProErr.Text = "发生了错误";
            RegisterStartupScript("showError", "<script>alert('" + lblProErr.Text + "');</script>");
        }
        else
        {
            int assetAid = int.Parse(Request.QueryString["Aid"]);
            var asset = MSrv.GetAssetByAid(assetAid);
            var assetMan = MSrv.GetAllMan();
            
            foreach (var i in assetMan)
            {
                AllMan.Items.Add(new ListItem(i.Uname, i.Uid.ToString()));
                
            }
            txtAid.Text = asset.Aid.ToString();
            txtAname.Text = asset.Aname.ToString();
        }
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        if(Request.QueryString["Aid"] == null)
        {
            lblProErr.Text = "发生错误了";
            RegisterStartupScript("showError", "<script>alert('" + lblProErr.Text + "');</script>");
        }
        else
        {
            int uid = int.Parse(AllMan.SelectedValue);
            var User =  MSrv.GetUnameByUid(uid);
            decimal phone = User.Telephone;

            string Uname = User.Uname;
            string Email = User.Email;
            MSrv.insertMaintenance(int.Parse(txtAid.Text), txtAname.Text.Trim(), txtMFname.Text.Trim(), decimal.Parse(txtFphone.Text), Uname, phone, DateTime.Now, txtDescribe.Text.Trim(), txtArea.Text, txtRdate.Text);
            lblProErr.Text = "该师傅没空";
            try
            {
                MailAddress from = new MailAddress("2153650885@qq.com", "××校园");
                MailAddress to = new MailAddress(Email);
                MailMessage message = new MailMessage(from, to);
                message.Subject = "您有一项代办任务";
                message.Body = Uname + "，您好" + "\n  " + "请您于" + txtRdate.SelectedItem.Text + "时间段" + "前往" + txtArea.Text + "修理资产:"+ txtAname.Text + "\n详细信息: " + txtDescribe.Text;
                SmtpClient c = new SmtpClient("smtp.qq.com", 587);
                c.EnableSsl = true;
                c.Credentials = new System.Net.NetworkCredential("2153650885@qq.com", "ngydszpipopedije");
                c.Send(message);
                Response.Redirect("AllMaintenance.aspx");
            }
            catch(Exception ex)
            {
                lblProErr.Text = "邮箱出错了!";
            }
        }  
    }

    protected void return_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllMaintenance.aspx");
    }


    
}